package com.ykdl.member.kid.fittool;

import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.adapters.FeedAdapter;
import com.ykdl.member.kid.beans.BabyProfileBean;
import com.ykdl.member.kid.beans.FeedListBean;
import com.ykdl.member.kid.beans.Nursing;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.util.CalendarUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;

public class FeedToolActivity extends BaseActivity implements 
    	OnClickListener, OnRefreshListener, OnRefreshListListener, OnItemLongClickListener{
	
	private static final String TAG = FeedToolActivity.class.getSimpleName();
	
	private RefreshListView mListView;
	private FeedAdapter mAdapter;
	private ProgressDialog mProgressDialog;
	
	private int cursor = 0;
	private int count = 20;
	private int type = 0;
	
	private FeedListTag mListTag;
	
	private ViewGroup mLayerAddPage;
	private RelativeLayout mLayerWheel;
	private TextView mTxtDate, mTxtSmallTitle;
	private CalendarUtil mCalendarUtil;
	
	private View mLayerBreast, mLayerFeeder, mLayerDiaper, mLayerSleep;
	
	private View mLayerMenu;
	private ImageView mImgArrow;
	private TextView mTxtAll, mTxtBreast, mTxtFeeder, mTxtDiaper, mTxtSleep;
	private TextView mTxtTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_feed_tool);
		initView();
		mProgressDialog = ProgressDialog.show(this, null, getString(R.string.loading_data), true, true);
		mListTag = new FeedListTag();
		Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
		mCalendarUtil = new CalendarUtil();
	}
	
	private void initView(){
		
		findViewById(R.id.btnBack).setOnClickListener(this);
		mTxtTitle = (TextView) findViewById(R.id.txtTitle);
		mImgArrow = (ImageView) findViewById(R.id.imgArrow);
		mImgArrow.setOnClickListener(this);
		
		findViewById(R.id.imgReport).setOnClickListener(this);
		
		mLayerMenu = findViewById(R.id.layerMenu);
		mLayerMenu.setOnClickListener(this);
		mTxtAll = (TextView) findViewById(R.id.txtAll);
		mTxtAll.setOnClickListener(this);
		mTxtBreast = (TextView) findViewById(R.id.txtBreast);
		mTxtBreast.setOnClickListener(this);
		mTxtFeeder = (TextView) findViewById(R.id.txtFeeder);
		mTxtFeeder.setOnClickListener(this);
		mTxtDiaper = (TextView) findViewById(R.id.txtDiaper);
		mTxtDiaper.setOnClickListener(this);
		mTxtSleep = (TextView) findViewById(R.id.txtSleep);
		mTxtSleep.setOnClickListener(this);
		
		
		mListView = (RefreshListView) findViewById(R.id.listView);
		mAdapter = new FeedAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setMoreButtoIsGon(true);
		mListView.setOnItemLongClickListener(this);
		
		mLayerAddPage = (ViewGroup)findViewById(R.id.layerAddPage);
		mLayerWheel = (RelativeLayout)findViewById(R.id.layerWheel);
		mLayerBreast = findViewById(R.id.layerBreastMilk);
		mLayerBreast.setOnClickListener(this);
		mLayerFeeder = findViewById(R.id.layerFeeder);
		mLayerFeeder.setOnClickListener(this);
		mLayerDiaper = findViewById(R.id.layerChangeDiaper);
		mLayerDiaper.setOnClickListener(this);
		mLayerSleep = findViewById(R.id.layerSleep);
		mLayerSleep.setOnClickListener(this);
		
	}
	
	private void activeAddView() {
		mLayerBreast.setClickable(true);
		mLayerFeeder.setClickable(true);
		mLayerDiaper.setClickable(true);
		mLayerSleep.setClickable(true);
	}
	
	private void antiActiveAddView() {
		mLayerBreast.setClickable(false);
		mLayerFeeder.setClickable(false);
		mLayerDiaper.setClickable(false);
		mLayerSleep.setClickable(false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgArrow:
			if (mLayerMenu.getVisibility() == View.VISIBLE) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
			} else {
				mImgArrow.setImageResource(R.drawable.title_uparrow);
				mLayerMenu.setVisibility(View.VISIBLE);
				antiActiveAddView();
			}
			break;
		case R.id.txtAll:
			if (mLayerMenu.getVisibility() == View.VISIBLE && type != 0) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
				mTxtTitle.setText("喂养记录");
				cursor = 0;
				type = 0;
				mProgressDialog.show();
				mAdapter.cleanData();
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			}
			break;
		case R.id.txtBreast:
			if (mLayerMenu.getVisibility() == View.VISIBLE && type != Nursing.NURSING_RECORD_TYPE_BREAST_FEEDING) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
				mTxtTitle.setText("母乳");
				cursor = 0;
				type = Nursing.NURSING_RECORD_TYPE_BREAST_FEEDING;
				mProgressDialog.show();
				mAdapter.cleanData();
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			}
			break;
		case R.id.txtFeeder:
			if (mLayerMenu.getVisibility() == View.VISIBLE && type != Nursing.NURSING_RECORD_TYPE_FEEDING) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
				mTxtTitle.setText("奶瓶");
				cursor = 0;
				type = Nursing.NURSING_RECORD_TYPE_FEEDING;
				mProgressDialog.show();
				mAdapter.cleanData();
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			}
			break;
		case R.id.txtDiaper:
			if (mLayerMenu.getVisibility() == View.VISIBLE && type != Nursing.NURSING_RECORD_TYPE_NAPPY) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
				mTxtTitle.setText("换尿布");
				cursor = 0;
				type = Nursing.NURSING_RECORD_TYPE_NAPPY;
				mProgressDialog.show();
				mAdapter.cleanData();
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			}
			break;
		case R.id.txtSleep:
			if (mLayerMenu.getVisibility() == View.VISIBLE && type != Nursing.NURSING_RECORD_TYPE_SLEEPING) {
				mImgArrow.setImageResource(R.drawable.title_downarrow);
				mLayerMenu.setVisibility(View.GONE);
				activeAddView();
				mTxtTitle.setText("睡眠");
				cursor = 0;
				type = Nursing.NURSING_RECORD_TYPE_SLEEPING;
				mProgressDialog.show();
				mAdapter.cleanData();
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			}
			break;
		case R.id.btnBack:
			finish();
			break;
		case R.id.layerBreastMilk:
			//母乳
			mLayerAddPage.setVisibility(View.VISIBLE);
			mListView.setEnabled(false);
			AddBreastFeeding abf = new AddBreastFeeding(this, mLayerAddPage, mListView);
			break;
		case R.id.layerFeeder:
			//奶瓶
			mLayerAddPage.setVisibility(View.VISIBLE);
			mListView.setEnabled(false);
			AddFeeding feed = new AddFeeding(this, mLayerAddPage, mListView);
			break;
		case R.id.layerChangeDiaper:
			//换尿布
			mLayerAddPage.setVisibility(View.VISIBLE);
			mListView.setEnabled(false);
			AddNappy nappy = new AddNappy(this, mLayerAddPage, mListView);
			break;
		case R.id.layerSleep:
			//睡眠
			mLayerAddPage.setVisibility(View.VISIBLE);
			mListView.setEnabled(false);
			AddSleeping sleep = new AddSleeping(this, mLayerAddPage, mListView);
			break;
		case R.id.imgReport:
			Intent intent = new Intent(this, FeedReportActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
		
	}
	
	private class FeedListTag implements ITag<FeedListBean> {

		@Override
		public void refreshUI(FeedListBean t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null) {
				if (t.getBaby_profile() != null) {
					mAdapter.setBaby(t.getBaby_profile());
				} else {
					mAdapter.setBaby(new BabyProfileBean());
				}
				List<Nursing> beans = t.getList();
				mAdapter.cleanData();
				mAdapter.addBeans(beans);
				mAdapter.notifyDataSetChanged();
				if (t.getNextCursor() == t.getTotalNumber()) {
					mListView.setMoreButtoIsGon(true);
				} else {
					cursor = t.getNextCursor();
				}
			} else {
				Toast.makeText(FeedToolActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			mProgressDialog.dismiss();
			Toast.makeText(FeedToolActivity.this, R.string.loaded_data_fail, Toast.LENGTH_SHORT).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			mProgressDialog.dismiss();
		}
		
	}
	
	private class AddFeedTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			if (t != null && t.getResult() == 0) {
				mProgressDialog = ProgressDialog.show(FeedToolActivity.this, null, getString(R.string.loading_data), true, true);
				cursor = 0;
				Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
			} else {
				Toast.makeText(FeedToolActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			if (parseError.getErrMsg() == null || TextUtils.equals(parseError.getErrMsg(), "") ) {
				Toast.makeText(FeedToolActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
				return false;
			} else {
				return true;
			}
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		mListView.onRefreshComplete();
	}
	
	private class DelDataTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null && t.getResult() == 0) {
				mAdapter.getBeans().remove(mDelBean);
				mAdapter.notifyDataSetChanged();
			} else {
				Toast.makeText(FeedToolActivity.this, "删除数据失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			mProgressDialog.dismiss();
			Toast.makeText(FeedToolActivity.this, "删除数据失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}
		
	}
	
	private static final int DIALOG_DELETE_DATA = 1;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DELETE_DATA:
			Dialog dialog = new AlertDialog.Builder(this).setMessage("是否删除?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
					if (mDelBean != null) 
						Network.getInstance().deleteFeedBean(mDelBean.getId(), new DelDataTag());
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).create();
			return dialog;

		default:
			return super.onCreateDialog(id);
		}
	}

	private Nursing mDelBean;

	@Override
	public void refreshList() {
		mProgressDialog = ProgressDialog.show(this, null, getString(R.string.loading_data), true, true);
		cursor = 0;
		Network.getInstance().requestFeedRecordList(cursor, count, type, mListTag);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		mDelBean = (Nursing) mAdapter.getItem(position - 1);
		showDialog(DIALOG_DELETE_DATA);
		return false;
	}
}
